﻿using System;

namespace Loong.Threading.Locks
{
    public class Lock : ILock
    {
        private readonly string _owner;
        private readonly Guid _lockId;
        private readonly LockFactoryBase _lockFactory;

        public string Resource { get; }

        public Lock(string resource, string owner, Guid lockId, LockFactoryBase lockFactory)
        {
            Resource = resource;

            _owner = owner;
            _lockId = lockId;
            _lockFactory = lockFactory;
        }

        private bool _isDisposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!_isDisposed)
            {
                if (disposing)
                {
                    _lockFactory.ReleaseLock(Resource, _owner, _lockId);
                }

                _isDisposed = true;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
